//删除
function deleteeach(id){
	if(confirm("确定要删除吗？")) {
		deletegq(id);
	}
}

function deletegq(id){
	$.ajax({
		url: urlcore + "/api/eachPicture/deleteEachPicture?id="+id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				loadMyEssay();
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//自动加载
$(function(){
	loadMyEssay();
});

function loadMyEssay() {

	var type = $("#eachtype")[0].options[$("#eachtype")[0].selectedIndex].value;
	var destatus = $("#destatus")[0].options[$("#destatus")[0].selectedIndex].value;

	//获取列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/eachPicture/getList?type="+type+"&isAdmin="+true+"&destatus="+destatus,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data,function(i,n){
					var id = n.id;
					var tm = new Date(n.gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss");
					var startTime = new Date(n.startTime).pattern("yyyy-MM-dd");
					var endTime = new Date(n.endTime).pattern("yyyy-MM-dd");
					var linkUrl = "'"+n.linkUrl+"'";
					var title = n.title==null?"":n.title;
					var content = n.content==null?"":n.content;
					var attachmentUrl =  "'"+n.attachmentUrl+"'";
					var thislist = 
'                                <tr class="footable-even" style="display: table-row;">'+
'                                    <td class="footable-visible">'+id+'</td>'+
'									 <td class="footable-visible"><img width="40px" height="40px" src="'+n.attachmentUrl+'"></td>'+
'                                    <td class="footable-visible">'+title+'</td>'+
'                                    <td class="footable-visible">'+content+'</td>'+
'                                    <td class="footable-visible">'+tm+'</td>'+
'                                    <td class="footable-visible">'+startTime+'--'+endTime+'</td>'+
'                                    <td class="footable-visible footable-last-column">'+
'							     		 <a class="btn btn-primary btn-xs" href="javascript:;" data-toggle="modal" data-target="#details" onclick="detailsThis('+linkUrl+','+attachmentUrl+')"> 详情</a>'+
'                                        <a class="btn btn-primary btn-xs" href="each_picture_add.html?id='+n.id+'&type='+n.type+'"> 修改</a>'+
'                                        <a class="btn btn-primary btn-xs" href="javascript:;" onclick="deleteeach('+n.id+')"> 删除</a>';
									 if(n.destatus== 0){
										thislist +=
									'		<a class="btn btn-primary btn-xs" href="javascript:;" onclick="release('+n.id+')">发布</a>';
									 }
									 thislist +=
'                                    </td>'+
'                                </tr>';
					$('#thislist').append(thislist);
				});
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});

}

//搜索
function searchusergq(){
	loadMyEssay();
}

//详情
function detailsThis(linkUrl, attachmentUrl) {
	var imgStr = '';
	if (attachmentUrl != null && attachmentUrl != '') {
		var imgarr = attachmentUrl.split("***");
		$.each(imgarr,function(index,value){
			imgStr += "<img style='padding: 5px 5px 5px 5px;' width='250px' height='250px' src='"+value+"'>";
		});
	}
	$('#img_details').html(imgStr);
	$('#linkUrl_details').text(linkUrl);
}

/**
 * 发布
 */
function release(id){
	$.ajax({
        url: urlcore + "/api/eachPicture/release?id="+id,
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success:function(data){
            if (data.success == true) {
            	location.reload();
            } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
        },
        error:function() {
            /* Act on the event */
            alert("error");
        }
    });
}